package cn.aitrox.ry.app.web.controller.user;

import cn.aitrox.ry.app.web.vo.user.in.CompanyListInVo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.service.api.dto.Company;
import cn.aitrox.ry.service.user.dto.in.CompanyCreateInDto;
import cn.aitrox.ry.service.user.dto.in.CompanyEditInDto;
import cn.aitrox.ry.service.user.feign.CompanyFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class CompanyController {

    @Autowired
    @Lazy
    private CompanyFeign companyFeign;

    @PostMapping("/company")
    public RespResult create(@RequestBody CompanyCreateInDto input) {
        return companyFeign.create(input);
    }

    @PutMapping("/company")
    public RespResult edit(@RequestBody CompanyEditInDto input) {
        return companyFeign.edit(input);
    }

    @PostMapping("/company/list")
    public RespResult<List<Company>> list(@RequestBody CompanyListInVo input) {
        List<Company> list = companyFeign.list();
        if (null != input.getStatus()) {
            list = list.stream().filter(x -> x.getStatus().equals(input.getStatus())).collect(Collectors.toList());
        }
        return RespResult.ok(list);
    }

}
